module mean_filter(
	clk			,
	rst_n			,
	data_in		,
	data_in_en	,
	data_out		,
	data_out_en	
);

	input clk;
	input rst_n;
	input [15:0] data_in;  //像素数据
	input data_in_en;
	output reg[15:0] data_out;
	output data_out_en;

	wire [15:0]line0;
	wire [15:0]line1;
	wire [15:0]line2;
	wire [18:0]result_data;
	
	reg [15:0] line0_data0,line0_data1,line0_data2;
	reg [15:0] line1_data0,line1_data1,line1_data2;
	reg [15:0] line2_data0,line2_data1,line2_data2;
	reg data_out_en0,data_out_en1,data_out_en2;
	
	line3x3 line3x3(
		.clken		(data_in_en),
		.clock		(clk),
		.shiftin		(data_in),
		.shiftout	(),
		.taps0x		(line0),
		.taps1x		(line1),
		.taps2x		(line2)
	);

	always @ (posedge clk or negedge rst_n)
	if (!rst_n)
		begin
			line0_data0 <= 16'd0;
			line0_data1 <= 16'd0;
			line0_data2 <= 16'd0;
			line1_data0 <= 16'd0;
			line1_data1 <= 16'd0;
			line1_data2 <= 16'd0;
			line2_data0 <= 16'd0;
			line2_data1 <= 16'd0;
			line2_data2 <= 16'd0;
			data_out_en0<= 1'b0;
			data_out_en1<= 1'b0;
			data_out_en2<= 1'b0;
		end
	else if (data_in_en)  //生成卷积核
		begin
			line0_data0 <= line0;
			line0_data1 <= line0_data0;
			line0_data2 <= line0_data1;
			line1_data0 <= line1;
			line1_data1 <= line1_data0;
			line1_data2 <= line1_data1;
			line2_data0 <= line2;
			line2_data1 <= line2_data0;
			line2_data2 <= line2_data1;
			data_out_en0<= data_in_en;
			data_out_en1<= data_out_en0;
			data_out_en2<= data_out_en1;
		end
		
	pa_8 pa_8_inst(
	  .clken(data_in_en),
	  .clock(clk),
	  .data0x(line0),
	  .data1x(line0_data0),
	  .data2x(line0_data1),
	  .data3x(line1),
	  .data4x(line1_data1),
	  .data5x(line2),
	  .data6x(line2_data0),
	  .data7x(line2_data1),
	  .result(result_data)
  );
			
always @ (posedge clk or negedge rst_n)
	if (!rst_n)
		data_out <= 16'd0;
	else if (data_in_en)
		data_out <= result_data[18:3]; //相当于右移三位 除以8  这里做一个平均
	else
		data_out <= data_out;
		
endmodule 